<template>
  <template v-if="'1'==os">
    <p>
      测试模式 <a-switch v-model:checked="onTest" :disabled="onWait" @change="StartTest"></a-switch>
    </p>
    <template v-for="(val,key) in osStatus['windows']" :key="key">
      <div style="margin: 15px 8px;float: left">
        {{key}}：
        <a-radio-group v-model:value="status[key]" :disabled="isDisable(key)" @change="onChange">
          <a-radio-button value="0">关</a-radio-button>
          <a-radio-button value="1">开</a-radio-button>
        </a-radio-group>
      </div>
    </template>
  </template>
  <template v-else-if="'2'==os">
    <template v-for="(val,key) in osStatus['linux']" :key="key">
      <div style="margin: 15px 8px;float: left">
        {{key}}：
        <a-radio-group v-model:value="status[key]" :disabled="isDisable(key)" @change="onChange">
          <a-radio-button value="0">关</a-radio-button>
          <a-radio-button value="2">维护</a-radio-button>
          <a-radio-button value="1">开</a-radio-button>
        </a-radio-group>
      </div>
    </template>
  </template>
</template>

<script lang="ts">
import {defineComponent, onBeforeUnmount, reactive, toRefs} from "vue"
import {OsName} from "@/views/terminal/utils"
import {ClientStatus, ClientTurnStatus} from "@/api/client"
import {Switch} from "ant-design-vue"

const osStatus={
  windows: {
    白名单: 'whitelist',
    黑名单: 'blacklist',
    文档管控: 'doc',
    端口控制: 'port',
    进站控制: 'inbound',
    出站控制: 'outbound'
  },
  linux:{
    白名单: 'whitelist',
    黑名单: 'blacklist',
    文档管控: 'doc',
    端口控制: 'port',
    进站控制: 'inbound',
    出站控制: 'outbound'
  }
}

export default defineComponent({
  name: "terminal-status",
  components:{
    [Switch.name]: Switch
  },
  props:{
    os:String,
    clientHash: String
  },
  setup(props){
    const state = reactive({
      status:{},
      onTest:false,
      onWait:true,
      timer:null as any
    })

    const beforChange = {}

    const getClientStatus = () => {
      clearInterval(state.timer)
      state.timer = setInterval( async () => {
        ClientStatus({'clienthash':props.clientHash}).then(
            (data)=>{
              const param={}
              param['os']=props.os
              const os=OsName[param['os']]
              Object.keys(osStatus[os]).forEach(key=> {state.status[key] = String(data[osStatus[os][key]])})
              Object.keys(osStatus[os]).forEach(i=> beforChange[i] = state.status[i])
              if('2'==state.status['白名单']){
                state.onTest=true
              }
              state.onWait=Object.values(state.status).includes('3')
            }
        )

      }, 5000)
      onBeforeUnmount(() => {
        clearInterval(state.timer)
      })
    }

    ~ async function () {
      Object.keys(osStatus).forEach(key => {
        Object.keys(osStatus[key]).forEach(i=> state.status[i] = '3')
      })
      if(null!=props.os) {
        ClientStatus({'clienthash': props.clientHash}).then(
            (data)=>{
              const param = {}
              param['os'] = props.os
              const os = OsName[param['os']]
              if(null!=os) {
                Object.keys(osStatus[os]).forEach(key => state.status[key] = String(data[osStatus[os][key]]))
                Object.keys(osStatus[os]).forEach(i => beforChange[i] = state.status[i])
                if ('2' == state.status['白名单']) {
                  state.onTest = true
                }
              }
              state.onWait=Object.values(state.status).includes('3')
            }
        )
      }
      getClientStatus()
    }()

    const onChange = async ()=>{
      const params={}
      params['clienthash']=props.clientHash
      params['os']=props.os
      const os=OsName[params['os']]
      Object.keys(osStatus[os]).forEach(i=>params[osStatus[os][i]] = state.status[i])
      //Object.keys(osStatus[os]).forEach(i=>{if(state.status[i]!=beforChange[i])state.status[i]='3'})
      state.onWait=Object.values(state.status).includes('3')
      await ClientTurnStatus(params)
    }

    const StartTest= async (isOn)=>{
      const params={}
      params['clienthash']=props.clientHash
      if(isOn) {
        Object.keys(state.status).forEach(i => state.status[i] = "2")
      }else{
        Object.keys(state.status).forEach(i => state.status[i] = "1")
      }
      Object.keys(osStatus["windows"]).forEach(i=>params[osStatus["windows"][i]] = state.status[i])
      state.onWait=Object.values(state.status).includes('3')
      await ClientTurnStatus(params)
    }

    const isDisable = (key)=>{
      if("1"==props.os){
        return '3' == (state.status[key] || "") || '2' == (state.status[key] || "")
      }
      if("2"==props.os) {
        return '3' == (state.status[key] || "")
      }
    }

    return{
      ...toRefs(state),
      OsName,
      osStatus,
      onChange,
      StartTest,
      isDisable
    }
  }
})
</script>

<style scoped>

</style>
